<?php
// -----------------------------------------------------------------------------
// include
// -----------------------------------------------------------------------------

include "../config/config.php";
include "../auth.php";
require '../vendor/autoload.php';

use Overtrue\ChineseCalendar\Calendar;

// -----------------------------------------------------------------------------
// 时间日期相关设置
// -----------------------------------------------------------------------------

/* 设置时区 */

date_default_timezone_set($region);

// -----------------------------------------------------------------------------
// 对象初始化
// -----------------------------------------------------------------------------

$calendar = new Calendar();

// -----------------------------------------------------------------------------
?>
<html lang="zh">
<head>
    <meta charset="utf-8">
    <title><?php echo $linkTitleEditRepeatTasks; ?></title>
    <base target="_self">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="../resources/bootstrap-5.2.0-beta1-dist/css/bootstrap.min.css">
</head>
<body>

<!-- ----------------------------------------------------------------------- -->

<script src="../resources/bootstrap-5.2.0-beta1-dist/js/bootstrap.min.js"></script>
<script src="../resources/jQuery/jquery-3.5.1.js"></script>
<?php include "../globalNavbar.php"; ?>

<!-- ----------------------------------------------------------------------- -->

<!--div:cintainer start-->
<div class="container container-fluid mb-5">

    <!-- 导航栏 开始 -->
    <div class="mt-3">
        <nav style="--bs-breadcrumb-divider: '>';" aria-label="breadcrumb">
            <ol class="breadcrumb">
                <li class="breadcrumb-item"><a class="text-decoration-none" href="<?php echo $homepage; ?>">首页</a></li>
                <li class="breadcrumb-item active"><?php echo $linkTitleEditRepeatTasks; ?></li>
            </ol>
        </nav>
    </div>
    <!-- 导航栏 结束 -->

    <div class="mt-2 mb-3">

        <div class="mt-0">
            <h3>每日重复</h3>
            <div class="list-group">
                <a href="../edit.php?from=daily" class="list-group-item list-group-item-action">编辑</a>
            </div>
        </div>

        <div class="mt-2">
            <h3>每周重复</h3>
            <div class="list-group mt-2">
                <a href="editWeeklyTasks.php" class="list-group-item list-group-item-action">编辑</a>
            </div>
        </div>

        <div class="mt-2">

            <h3>每月重复 - 阳历</h3>
            <div class="list-group mt-2">
                <a href="editMonthlyTasks.php" class="list-group-item list-group-item-action">编辑</a>
            </div>

        </div>

        <div class="mt-2">

            <h3>每月重复 - 农历</h3>
            <div class="list-group mt-2">
                <a href="editMonthlyLunarTasks.php" class="list-group-item list-group-item-action">编辑</a>
            </div>

        </div>

        <div class="mt-2">

            <h3>每年重复 - 阳历</h3>

            <div class="mb-2">

                <div class="mt-2">
                    <select name="month" id="selectMonth" class="form-control">
                        <?php
                        for ($i = 1; $i <= 12; $i++) {
                            if ($i < 10) {
                                $i = "0" . $i;
                            }
                            echo <<<BBB
                                <option value="{$i}">{$i}月</option>
                            BBB;
                        }
                        ?>
                    </select>
                </div>

                <div class="mt-2">
                    <select name="dayOfMonth" id="selectDayOfMonth" class="form-control">
                        <?php
                        for ($i = 1; $i <= 31; $i++) {
                            if ($i < 10) {
                                $i = "0" . $i;
                            }
                            echo <<<BBB
                                <option value="{$i}">{$i}号</option>
                            BBB;
                        }
                        ?>
                    </select>
                </div>

                <div class="mt-2">
                    <button type="button" id="btnEditYearly" class="btn btn-outline-secondary">编辑</button>
                </div>

            </div>

        </div>

        <div class="mt-2">
            <h3>每年重复 - 农历</h3>
            <div class="mt-2">

                <div class="mt-2">
                    <select name="isLeap" id="selectIsLeap" class="form-control">
                        <option value="false">平月</option>
                        <option value="true">闰月</option>
                    </select>
                </div>

                <div class="mt-2">
                    <select name="monthLunar" id="selectMonthLunar" class="form-control">
                        <?php
                        for ($i = 1; $i <= 12; $i++) {
                            if ($i < 10) {
                                $i = "0" . $i;
                            }
                            $chinaMonth = $calendar->toChinaMonth($i);
                            echo <<<BBB
                                <option value="{$i}">{$chinaMonth}</option>
                            BBB;
                        }
                        ?>
                    </select>
                </div>

                <div class="mt-2">
                    <select name="dayOfMonthLunar" id="selectDayOfMonthLunar" class="form-control">
                        <?php
                        for ($i = 1; $i <= 30; $i++) {
                            if ($i < 10) {
                                $i = "0" . $i;
                            }
                            $chinaDay = $calendar->toChinaDay($i);
                            echo <<<BBB
                                <option value="{$i}">{$chinaDay}</option>
                            BBB;
                        }
                        ?>
                    </select>
                </div>

                <div class="mt-2">
                    <button type="button" id="btnEditYearlyLunar" class="btn btn-outline-secondary">编辑</button>
                </div>

            </div>
        </div>

    </div>

</div>

<!-- ----------------------------------------------------------------------- -->

<script>

    $("#btnEditYearly").on("click", function () {

        var month = $("#selectMonth").val()
        var dayOfMonth = $("#selectDayOfMonth").val()
        var monthAndDay = month + dayOfMonth

        var url = "../edit.php?from=yearly&monthAndDay=" + monthAndDay
        window.open(url, "_self")

    })

    $("#btnEditYearlyLunar").on("click", function () {

        var isLeap = $("#selectIsLeap").val();
        var mark = "";
        if (isLeap == "true") {
            mark = "-"
        }

        var monthLunar = $("#selectMonthLunar").val();
        var dayOfMonthLunar = $("#selectDayOfMonthLunar").val();
        var monthAndDay = mark + monthLunar + dayOfMonthLunar;

        var url = `../edit.php?from=yearlyLunar&monthAndDay=${monthAndDay}`;
        window.open(url, "_self")

    })

</script>

<!----------------------------------------------------------------------------->

<!--网页加载时执行-->
<script>

    <?php

        $year = date("Y");
        $month = date("m");
        $dayOfMonth = date("d");
        $result = $calendar->solar($year, $month, $dayOfMonth);

        $isLeap = "false";
        if ($result["is_leap"] == true) {
            $isLeap = "true";
        }

        $monthLunar = $result["lunar_month"];
        $dayOfMonthLunar = $result["lunar_day"];

        echo <<<BBB
        
            $("#selectMonth").val("$month");
            $("#selectDayOfMonth").val("$dayOfMonth");
            
            $("#selectIsLeap").val("{$isLeap}");
            $("#selectMonthLunar").val("$monthLunar");
            $("#selectDayOfMonthLunar").val("$dayOfMonthLunar");
            
        BBB;

    ?>

</script>

<!----------------------------------------------------------------------------->

</body>
</html>
